package com.eurasia.assignment.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eurasia.assignment.controller.dto.CreateProjectRequest;
import com.eurasia.assignment.controller.dto.PageResponse;
import com.eurasia.assignment.controller.dto.ProjectResponse;
import com.eurasia.assignment.controller.dto.ProjectSearchRequest;
import com.eurasia.assignment.domain.Project;
import com.eurasia.assignment.service.ProjectService;

@RestController
@RequestMapping("/projects")
public class ProjectController {

    private final ProjectService projectService;

    @Autowired
    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }

    @GetMapping("")
    public ResponseEntity<List<ProjectResponse>> getAllProjects() {
        List<Project> projects = projectService.getAllProjects();
        List<ProjectResponse> response = projects.stream()
            .map(ProjectResponse::fromDomain)
            .collect(Collectors.toList());
        return ResponseEntity.ok(response);
    }

    @GetMapping("/{id}")
    public ResponseEntity<ProjectResponse> getProjectById(@PathVariable Long id) {
        try {
            Project project = projectService.getProjectById(id);
            return ResponseEntity.ok(ProjectResponse.fromDomain(project));
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("")
    public ResponseEntity<ProjectResponse> createProject(@RequestBody CreateProjectRequest request) {
        // Convert request DTO to domain object (assuming Project has a suitable constructor or factory method)
        // This part might need adjustment based on your domain logic
        Project newProject = new Project();
        newProject.setName(request.getName());
        newProject.setCourse(request.getCourse());
        newProject.setType(request.getType());
        // Set other default fields if necessary in the domain or service layer
        
        Project createdProject = projectService.createProject(newProject);
        return ResponseEntity.status(HttpStatus.CREATED).body(ProjectResponse.fromDomain(createdProject));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ProjectResponse> updateProject(@PathVariable Long id, @RequestBody Project projectDetails) {
        try {
            Project updatedProject = projectService.updateProject(id, projectDetails);
            return ResponseEntity.ok(ProjectResponse.fromDomain(updatedProject));
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProject(@PathVariable Long id) {
        try {
            projectService.deleteProject(id);
            return ResponseEntity.noContent().build();
        } catch (RuntimeException e) {
            return ResponseEntity.notFound().build();
        }
    }
    
    @PostMapping("/search")
    public ResponseEntity<PageResponse<ProjectResponse>> searchProjects(
            @RequestBody ProjectSearchRequest searchRequest,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<Project> projectPage = projectService.searchProjectsWithPagination(searchRequest, page, size);
        List<ProjectResponse> projectResponses = projectPage.getContent().stream()
            .map(ProjectResponse::fromDomain)
            .collect(Collectors.toList());
        PageResponse<ProjectResponse> response = PageResponse.of(
            projectResponses,
            projectPage.getNumber(),
            projectPage.getSize(),
            projectPage.getTotalElements()
        );
        return ResponseEntity.ok(response);
    }
} 